import {
  Controller,
  UploadedFile,
  Post,
  UseInterceptors,
} from '@nestjs/common';
import { FileInterceptor } from '@nestjs/platform-express';
import { MinioService } from '../minio/minio.service';
import { v4 as uuidv4 } from 'uuid';
import { hashTimestamp } from '../../utils/tool';
import { Public } from "../../global/decorator/public.decorator";

@Controller('minio')
export class MinioController {
  constructor(private readonly minioService: MinioService) {}

  @Post('upload')
  @UseInterceptors(FileInterceptor('file'))
  async uploadFile(@UploadedFile() file: any) {
    const hash = hashTimestamp(new Date().getTime());
    const suffixSplit = file.mimetype.split('/');
    const suffix =
      suffixSplit.length !== 1
        ? suffixSplit[suffixSplit.length - 1]
        : suffixSplit[0];
    return await this.minioService.uploadFile(
      'blog',
      `${hash}${uuidv4().split('-').join('')}.${suffix}`,
      file.buffer,
    );
  }
}
